package com.mrs.gauss.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrs.gauss.common.ErrorCode;
import com.mrs.gauss.constant.CommonConstant;
import com.mrs.gauss.exception.BusinessException;
import com.mrs.gauss.exception.ThrowUtils;
import com.mrs.gauss.model.entity.Generator;
import com.mrs.gauss.model.entity.MrsUser;
import com.mrs.gauss.model.generator.GeneratorQueryRequest;
import com.mrs.gauss.model.vo.GeneratorVO;
import com.mrs.gauss.model.vo.UserVO;
import com.mrs.gauss.service.GeneratorService;
import com.mrs.gauss.service.dao.MrsUserDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 帖子服务实现
 *
 */
@Service
@Slf4j
public class GeneratorServiceImpl  implements GeneratorService {

    @Resource
    private MrsUserDao mrsUserDao;
    @Resource
    private MrsUserService userService;

    @Override
    public void validGenerator(Generator generator, boolean add) {
        if (generator == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = generator.getName();
        String description = generator.getDescription();

        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(name, description), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(name) && name.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
        if (StringUtils.isNotBlank(description) && description.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "描述过长");
        }
    }

    @Override
    public GeneratorVO getGeneratorVO(Generator generator, HttpServletRequest request) {
        return null;
    }


    @Override
    public Page<GeneratorVO> getGeneratorVOPage(Page<Generator> generatorPage, HttpServletRequest request) {
        List<Generator> generatorList = generatorPage.getRecords();
        Page<GeneratorVO> generatorVOPage = new Page<>(generatorPage.getCurrent(), generatorPage.getSize(), generatorPage.getTotal());
        if (CollUtil.isEmpty(generatorList)) {
            return generatorVOPage;
        }
        // 1. 关联查询用户信息
        Set<Long> userIdSet = generatorList.stream().map(Generator::getUserId).collect(Collectors.toSet());
        Map<Long, List<MrsUser>> userIdUserListMap = mrsUserDao.userList(userIdSet).stream()
                .collect(Collectors.groupingBy(MrsUser::getId));
        // 填充信息
        List<GeneratorVO> generatorVOList = generatorList.stream().map(generator -> {
            GeneratorVO generatorVO = GeneratorVO.objToVo(generator);
            Long userId = generator.getUserId();
            MrsUser user = null;
            if (userIdUserListMap.containsKey(userId)) {
                user = userIdUserListMap.get(userId).get(0);
            }
            generatorVO.setUser(userService.getUserVO(user));
            return generatorVO;
        }).collect(Collectors.toList());
        generatorVOPage.setRecords(generatorVOList);
        return generatorVOPage;
    }

    @Override
    public boolean saveBatch(Collection<Generator> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Generator> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Generator> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Generator entity) {
        return false;
    }

    @Override
    public Generator getOne(Wrapper<Generator> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Generator> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Generator> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<Generator> getBaseMapper() {
        return null;
    }

    @Override
    public Class<Generator> getEntityClass() {
        return null;
    }
}




